using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


    public partial class AdminPages_ViewInstructorInfo : System.Web.UI.Page
    {
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();

            }
        }
        private void BindDetails()
        {
            //Define data objects
            SqlConnection conn;
            SqlCommand comm;
            SqlDataReader reader;
            int id = Convert.ToInt16(instructorDetails.DataKey.Value.ToString());

            string query = @"SELECT " +
                    "first_Name AS 'FIRST', " +
                    "last_Name AS 'LAST', " +
                    "address AS 'ADDRESS', " +
                    "city AS 'CITY', " +
                    "state AS 'STATE', " +
                    "zip AS 'ZIP', " +
                    "phone_Num AS 'PHONE', " +
                    "email AS 'EMAIL', " +
                    "office_Num AS 'OFFICE', " +
                    "instructor_Id AS 'ID' " +
                "FROM " +
                    "Instructor " +
                "WHERE " +
                    "instructor_Id = @Id";

            //Read the connection string from web.config
            string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

            //Initialize connection
            conn = new SqlConnection(connectionString);

            //Create command
            comm = new SqlCommand(query, conn);

            //Add employee id parameter
            comm.Parameters.AddWithValue("@Id", id);

            //Enclose database code in Try-Catch-Finally block
            try
            {
                //Open the connection
                conn.Open();

                //Execute the command
                reader = comm.ExecuteReader();
                instructorDetails.Visible = true;

                //Fill the grid with data
                instructorDetails.DataSource = reader;
                instructorDetails.DataBind();

                //Close the reader
                reader.Close();
            }
            finally
            {
                //Close the connection
                conn.Close();
            }

        }
        private void BindDetails(DataListCommandEventArgs e)
        {
            //Define data objects
            SqlConnection conn;
            SqlCommand comm;
            SqlDataReader reader;
            int id = Convert.ToInt16(e.CommandArgument);

            string query = @"SELECT " +
                    "first_Name AS 'FIRST', " +
                    "last_Name AS 'LAST', " +
                    "address AS 'ADDRESS', " +
                    "city AS 'CITY', " +
                    "state AS 'STATE', " +
                    "zip AS 'ZIP', " +
                    "phone_Num AS 'PHONE', " +
                    "email AS 'EMAIL', " +
                    "office_Num AS 'OFFICE', " +
                    "instructor_Id AS 'ID' " +
                "FROM " +
                    "Instructor " +
                "WHERE " +
                    "instructor_Id = @Id";

            //Read the connection string from web.config
            string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

            //Initialize connection
            conn = new SqlConnection(connectionString);

            //Create command
            comm = new SqlCommand(query, conn);

            //Add employee id parameter
            comm.Parameters.AddWithValue("@Id", id);

            //Enclose database code in Try-Catch-Finally block
            try
            {
                //Open the connection
                conn.Open();

                //Execute the command
                reader = comm.ExecuteReader();
                instructorDetails.Visible = true;

                //Fill the grid with data
                instructorDetails.DataSource = reader;
                instructorDetails.DataKeyNames = new string[] { "ID" };
                instructorDetails.DataBind();

                //Close the reader
                reader.Close();
            }
            finally
            {
                //Close the connection
                conn.Close();
            }

        }

        protected void instructorList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            instructorDetails.ChangeMode(DetailsViewMode.Edit);
            BindDetails(e);
        }
        protected void instructorDetails_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            //Change current mode to the selected one
            instructorDetails.ChangeMode(e.NewMode);
            //Rebind the details view
            BindDetails();
        }
        protected void instructorDetails_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            //Read the employee id from the DetailsView object
            //int selectedCustomer = instructorDetails.DataKey.Value;

            //Find the TextBox controls with updated data
            TextBox newFirstTextBox = (TextBox)instructorDetails.FindControl("editFirstName");
            TextBox newLastTextBox = (TextBox)instructorDetails.FindControl("editLastName");
            TextBox newAddressTextBox = (TextBox)instructorDetails.FindControl("editAddress");
            TextBox newCityTextBox = (TextBox)instructorDetails.FindControl("editCity");
            TextBox newStateTextBox = (TextBox)instructorDetails.FindControl("editState");
            TextBox newZipTextBox = (TextBox)instructorDetails.FindControl("editZip");
            TextBox newPhoneTextBox = (TextBox)instructorDetails.FindControl("editPhone");
            TextBox newEmailTextBox = (TextBox)instructorDetails.FindControl("editEmail");
            TextBox newOfficeTextBox = (TextBox)instructorDetails.FindControl("editOffice");

            //Extract the updated data from the text boxes
            string newFirst = newFirstTextBox.Text;
            string newLast = newLastTextBox.Text;
            string newAddress = newAddressTextBox.Text;
            string newCity = newCityTextBox.Text;
            string newState = newStateTextBox.Text;
            string newZip = newZipTextBox.Text;
            string newPhone = newPhoneTextBox.Text;
            string newEmail = newEmailTextBox.Text;
            string newOffice = newOfficeTextBox.Text;

            //Define data objects
            SqlConnection conn;
            SqlCommand comm;

            //Initialize the connection
            string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

            //Initialize connection
            conn = new SqlConnection(connectionString);
            
            string update = @"UPDATE Instructor SET last_Name = @last, first_Name = @first, address = @address, " +
                "city = @city, state = @state, zip = @zip, phone_Num = @phone, email = @email, office_Num = @office " +
                "WHERE instructor_ID = @ID";

            int id = Convert.ToInt16(instructorDetails.DataKey.Value);

            //Create command
            comm = new SqlCommand(update, conn);

            comm.Parameters.AddWithValue("@ID", id);
            comm.Parameters.AddWithValue("@last", newLast);
            comm.Parameters.AddWithValue("@first", newFirst);
            comm.Parameters.AddWithValue("@address", newAddress);
            comm.Parameters.AddWithValue("@city", newCity);
            comm.Parameters.AddWithValue("@state", newState);
            comm.Parameters.AddWithValue("@zip", newZip);
            comm.Parameters.AddWithValue("@phone", newPhone);
            comm.Parameters.AddWithValue("@email", newEmail);
            comm.Parameters.AddWithValue("@office", newOffice);

            //Enclose database code in Try-Catch-Finally block
            try
            {
                //Open the connection
                conn.Open();

                //Execute the command
                comm.ExecuteNonQuery();
            }
            finally
            {
                //Close the connection
                conn.Close();
            }

            //Exit edit mode
            instructorDetails.ChangeMode(DetailsViewMode.ReadOnly);

            //Reload the title view
            BindDetails();
            BindList();
        }
        protected void BindList()
        {
           // Define data objects
                SqlConnection conn;
                SqlCommand comm;
                SqlDataReader reader;

                string query = @"SELECT
                                        first_Name AS FIRST, 
                                        last_Name AS LAST, 
                                        address AS ADDRESS, 
                                        state AS STATE, 
                                        zip AS ZIP, 
                                        phone_Num AS PHONE, 
                                        city AS CITY, 
                                        email AS EMAIL,
                                        office_Num AS OFFICE,
                                        instructor_ID AS ID
                                 FROM            
                                        Instructor;";

                // Read the connection string from Web.config
                string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

                // Initialize connection
                conn = new SqlConnection(connectionString);

                // Create command
                comm = new SqlCommand(query, conn);

                // Enclose database code in Try-Catch-Finally
                try
                {
                    // Open the connection
                    conn.Open();

                    // Execute the command
                    reader = comm.ExecuteReader();

                    // Fill the grid with data
                    instructorList.DataSource = reader;
                    instructorList.DataBind();

                    // Close the reader
                    reader.Close();
                }
                finally
                {
                    comm = null;
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    conn = null;
                }
            }
        }

